﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Jzy.BLL.Project;
using Jzy.Core.Common;
using System.Data;
using Jzy.Model.Project;
using Frame.Lib.Web;
using Frame.Lib.Utilities;

namespace Jzy.Web.Project
{
    public partial class ArchiveList : PageList
    {
        private ProjectBO _prj = new ProjectBO();
        private PagerSortSetting _setting;
        private string encryptStatus;

        protected void Page_Load(object sender, EventArgs e)
        {
            encryptStatus = Encrypt((int)PrjStatus.归档);

            _setting = this.Footer1.InitPager(new BindViewHandler(BindView));
            if (!IsPostBack)
            {
                BindYear();
                BindView();
            }
            InitSelectID(hidSelectIDs);
            JSUtil.SetTableHeight("list", 140);
            AddAttr();
        }

        private void BindYear()
        {
            _prj.BindYear(drpYear, PrjStatus.归档);
        }

        private void AddAttr()
        {
            JSUtil.ClickBtn(lnkRestart,btnRestart);
            JSUtil.ClickBtn(lnkDelete, btnDelete);
            JSUtil.DelConfirm(btnDelete, "项目");

            btnRestart.Attributes.Add("onclick", "return btnRestart_Click()");

        }
        private void BindView()
        {
            lv.DataKeyNames = new string[] { "ID", "Status" };
            InitLV(lv, lvHeader);
            DataTable dt = null;
            btnPre.Text = btnPre.CommandArgument = (int.Parse(drpYear.SelectedValue) - 1).ToString();
            btnNext.Text = btnNext.CommandArgument = (int.Parse(drpYear.SelectedValue) + 1).ToString();

            if (int.Parse(btnPre.CommandArgument) < int.Parse(drpYear.Items[0].Value))
            {
                btnPre.Enabled = false;
            }
            else
            {
                btnPre.Enabled = true;
            }

            if (int.Parse(btnNext.CommandArgument) > int.Parse(drpYear.Items[drpYear.Items.Count - 1].Value))
            {
                btnNext.Enabled = false;
            }
            else
            {
                btnNext.Enabled = true;
            }
            
            dt = _prj.GetDataTable(drpYear.SelectedValue, (int)PrjStatus.归档);
            

            lv.DataSource = _setting.GetDataView(dt);
            lv.DataBind();
        }
        protected void drpYear_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindView();

        }
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (strSelectIDs != "")
            {
                _prj.Delete(strSelectIDs, true);
                BindYear();
                BindView();
            }
        }
        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            BindYear();
            BindView();
        }
        protected void btnPre_Click(object sender, EventArgs e)
        {
            drpYear.SelectedValue = btnPre.CommandArgument;
            BindView();
        }
        protected void btnNext_Click(object sender, EventArgs e)
        {
            drpYear.SelectedValue = btnNext.CommandArgument;
            BindView();
        }

        protected void btnRestart_Click(object sender, EventArgs e)
        {
            List<int> lstSelectID = StrUtil.SplitToInt(strSelectIDs);
            _prj.SetStatus(lstSelectID, PrjStatus.启动);
            BindYear();
            BindView();
        }

        protected void lv_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            if (e.Item.ItemType == ListViewItemType.DataItem)
            {
                ListViewDataItem item = (ListViewDataItem)e.Item;
                string strEncryptID = StrUtil.EncryptUrl(lv.DataKeys[item.DataItemIndex]["ID"].ToString());
                HyperLink lnkName = (HyperLink)e.Item.FindControl("lnkName");
                lnkName.NavigateUrl = "TabList.aspx?PrjID=" + strEncryptID + "&Status=" + encryptStatus;
            }
        }
    }
}
